package com.application.lucky.request;

import android.content.Intent;
import android.os.Looper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.application.lucky.Lucky;
import com.application.lucky.activity.LoginActivity;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.utils.TopActivityManager;
import com.application.mylibrary.config.Common;
import com.application.mylibrary.utils.ToastUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.HttpCallback;
import com.baselib.libnetwork.net.model.ResultResponse;

import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.qunar.im.ui.BuildConfig;
import com.qunar.im.ui.view.bigimageview.tool.utility.ui.ToastUtil;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketException;
import java.net.UnknownHostException;

import retrofit2.HttpException;

/**
 * @author lixiaoyu
 * @version v1.0
 * @description
 * @since 2017/10/19
 * <p>
 * <p>
 * 添加说明
 * 1.如果不希望底层解析 T 可以传 String 返回的就是 json 元数据
 * <p>
 * 2.如果只希望获取 code 值，T 可以传 BaseResponse
 */

public abstract class CallBackEx<T> extends HttpCallback<T> {

    @Override
    protected void OnFailureNeedData(int code, T t) {
        switch (code) {
            case Common.ErrorCode.LOGIN_LOGINKEY_UNAVAILABLE:
                UserManagers.getInstance().loginOut();

                TopActivityManager.getInstance().exit();
                Intent login = new Intent(Lucky.getApplication(), LoginActivity.class);
                login.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Lucky.getApplication().startActivity(login);
                break;
            default:
                onFailed(code);
                break;
        }

        onFailureNeedData(code, t);
    }

    public abstract void onFailed(int code);

    /**
     * 服务器返回非正确code码，同时包含数据
     *
     * @param code
     * @param t
     */
    public void onFailureNeedData(int code, T t) {

    }

    @Override
    public void onException(ResultResponse<T> response) {
        if (response != null) {
            Throwable e = response.getException();

            if (e instanceof UnknownHostException
                    || e instanceof ConnectException
                    || e instanceof HttpException
                    || e instanceof SocketException) {
                onNetError();
            } else if (e instanceof JsonParseException
                    || e instanceof JSONException) {
                if (BuildConfig.DEBUG) {
                    ToastUtils.show("解析异常");
                }

            }
        }
    }

    public void onNetError() {

    }

    @Override
    public T beforeParseJson(String json) {
        JSONObject object = JSON.parseObject(json);
        if (object.getInteger("code") == 99) {
            if (Looper.getMainLooper() == Looper.myLooper()) {
                ToastUtils.show("该账号在其他设备上登录，请重新登录！");
            }

            UserManagers.getInstance().loginOut();
            TopActivityManager.getInstance().exit();
            Intent login = new Intent(Lucky.getApplication(), LoginActivity.class);
            login.putExtra("isout", true);
            login.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Lucky.getApplication().startActivity(login);
        }
        return super.beforeParseJson(json);
    }
}
